let customeraddModule = (function(){
    let $username = $('.username'),
        $man = $('#man'),
        $woman = $('#woman'),
        $useremail = $('.useremail'),
        $userphone = $('.userphone'),
        $userqq = $('.userqq'),
        $userweixin = $('.userweixin'),
        $userjob = $('.userjob'),
        $useraddress = $('.useraddress'),
        $spanname = $('.spanname'),
        $spanemail = $('.spanemail'),
        $spanphone = $('.spanphone'),
        $submit = $('.submit');
    let customerId = null;
    // 编辑客户（首先获取客户信息）
    async function getUserInfo(){
        let result = await axios.get('/customer/info',{
            params:{
                customerId
            }
        })
        if(result.code == 0){
            bindHTML(result.data);
            return;
        }
        alert('小主，网络繁忙，请稍后重试');
        return;
    }
    function bindHTML(data){
        let {
            id,
            name,
            sex,
            email,
            phone,
            QQ,
            weixin,
            type,
            address,
            userId,
            userName
        } = data;
        $username.val(name);
        sex ==0 ? $man.prop('checked'):$woman.prop('checked');
        $useremail.val(email)
        $userphone.val(phone)
        $userqq.val(QQ)
        $userweixin.val(weixin)
        $userjob.val(type)
        $useraddress.val(address);
    }
    function handle(){
        $username.blur(checkName)
        $useremail.blur(checkEmail)
        $userphone.blur(checkPhone)
        // 点击提交 男0 女1
        $submit.click(async function(e){
            // 获取用户输入的信息
            let params = {
                name:$username.val().trim(),
                sex:$man.prop('checked')?'0':'1',
                email:$useremail.val().trim(),
                phone:$userphone.val().trim(),
                QQ:$userqq.val().trim(),
                weixin:$userweixin.val().trim(),
                type:$userjob.val(),
                address:$useraddress.val().trim()
            }
            
            if(checkName() && checkEmail() && checkPhone()){
                if(customerId){
                    // 修改用户信息
                    params['customerId'] = customerId;
                    console.log(params)
                    let result = axios.post('/customer/update',params);
                    console.log(result.code)
                    if(result.code == 0){
                        alert('小主，已成功为您修改一条客户信息~');
                        window.history.go(-1);//返回上一页
                        return;
                    }
                    alert('小主，网络繁忙，请稍后重试');
                    return;
                }else{
                    // 新增客户信息
                    let result = await axios.post('/customer/add',params)
                    if(result.code == 0){
                        alert('小主，已成功为您新增一条客户信息~')
                        return;
                    }
                    alert('小主，网络繁忙，请稍后重试');
                    return;
                }     
            }
        })
    }
    // 表单校验
    function checkName(){
        let val = $username.val().trim();
        if(val === ''){
            $spanname.html('小主，用户名不能为空~')
            return false;
        }
        else if(!/^[\u4E00-\u9FA5]{2,10}(·[\u4E00-\u9FA5]{2,10}){0,2}$/.test(val)){
            $spanname.html('小主，请输入正确的用户名【2-10位汉字】~~')
            return false;
        }else{
            $spanname.html('')
            return true;
        }
        

    }
    function checkEmail(){
        let val = $useremail.val().trim();
        if(val === ''){
            $spanemail.html('小主，邮箱不能为空~')
            return false;
        }
        else if(!/^\w+((-\w+)|(\.\w+))*@[a-zA-Z0-9]+((\.|-)[a-zA-A0-9]+)*\.[a-zA-Z0-9]+$/.test(val)){
            $spanemail.html('请输入正确的邮箱~')
            return false;
        }else{
            $spanemail.html('')
            return true;
        }
        

    }
    function checkPhone(){
        let val = $userphone.val().trim();
        if(val === ''){
            $spanphone.html('小主，手机号不能为空~')
            return false;
        }
        else if(!/^1\d{10}$/.test(val)){
            $spanphone.html('小主，请输入手机号~')
            return false;
        }else{
            $spanphone.html('')
            return true;
        }
        
    }

    return {
        init(){
            // 获取到传递的客户id，从而获取客户的基本信息
            let params = window.location.href.queryUrlParams();
            // 区分 新增 还是 编辑
            if(params.hasOwnProperty('id')){
                customerId = params.id;
                getUserInfo()
            }
            handle()
            
        }
    }
}())
customeraddModule.init();